package com.cango.qutz.web.auth.org.service.impl;

import java.util.Date;
import java.util.List;

import com.cango.qutz.web.auth.org.dao.IOrgDao;
import com.cango.qutz.web.auth.org.service.IOrgService;
import com.cango.qutz.web.entity.OrgEntity;
import com.cango.framework.base.pagination.PageInfo;
import com.cango.framework.base.pagination.PaginationResult;
import com.cango.qutz.web.auth.org.dao.IOrgDao;
import com.cango.qutz.web.auth.org.service.IOrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("orgService")
public class OrgServiceImpl implements IOrgService {
    /** 机构sql的命名空间 */
    private static final String SQL_NAMESPACE = "org.";

    /** 新增机构的sql id */
    private static final String SQL_INSERT = "insertOrg";

    /** 更新机构的sql id */
    private static final String SQL_UPDATE = "updateOrg";

    /** 删除机构的sql id */
    private static final String SQL_DELETE = "deleteOrg";

    /** 根据主键查询机构的sql id */
    private static final String SQL_QUERY_BY_ID = "queryOrgById";

    /** 查询机构的sql id */
    private static final String SQL_QUERY_FOR_LIST = "queryOrgs";

    /** 分页查询机构的sql id */
    private static final String SQL_QUERY_BY_PAGE = "queryOrgsByPage";

    /** 根据机构代码查询数量 id */
    private static final String SQL_QUERY_COUNT_BY_CODE = "queryCountByCode";

    /** 根据机构代码查询数量 id */
    private static final String SQL_QUERY_ORG_BY_CODE = "queryOrgByCode";
    
    /** 查询机构所有分区 包括总部 */
    private static final String SQL_QUERY_FOR_LIST_REGION = "queryOrgsRegion";
    @Autowired
    private IOrgDao orgDao;

    @Override
    public int insert(OrgEntity org) {
        // 设置机构信息的初始值
        org.setIsDel(0);
        org.setOrgPath(org.getParentCode() + ">>" + org.getOrgCode());
        int id = orgDao.insert(SQL_NAMESPACE + SQL_INSERT, org);
        return id;
    }

    @Override
    public int update(OrgEntity org) {
        OrgEntity orgEdit = this.queryObjectById(org.getOrgId());
        // 设置修改的属性
        orgEdit.setUpdateUser(org.getUpdateUser());
        orgEdit.setUpdateTime(new Date());
        orgEdit.setOrgName(org.getOrgName());
        orgEdit.setSimpleName(org.getSimpleName());
        orgEdit.setRemark(org.getRemark());
        orgEdit.setStatus(org.getStatus());
        return orgDao.update(SQL_NAMESPACE + SQL_UPDATE, orgEdit);
    }

    @Override
    public int delete(OrgEntity org) {
        return orgDao.delete(SQL_NAMESPACE + SQL_DELETE, org);
    }

    @Override
    public OrgEntity queryObjectById(Integer orgId) {
        return orgDao.queryObjectById(SQL_NAMESPACE + SQL_QUERY_BY_ID, orgId);
    }

    @Override
    public List<OrgEntity> queryList(Object param) {
        return orgDao.queryList(SQL_NAMESPACE + SQL_QUERY_FOR_LIST, param);
    }

    @Override
    public PaginationResult<OrgEntity> queryListByPage(OrgEntity param, PageInfo pageinfo) {
        return orgDao.queryListByPage(SQL_NAMESPACE + SQL_QUERY_BY_PAGE, param, pageinfo);
    }

    @Override
    public boolean validOrgCode(String orgCode) {
        int count = this.orgDao.queryCountByCode(SQL_NAMESPACE + SQL_QUERY_COUNT_BY_CODE, orgCode);
        return count > 0 ? false : true;
    }

    @Override
    public OrgEntity queryOrgByCode(String orgCode) {
        return this.orgDao.queryForObject(SQL_NAMESPACE + SQL_QUERY_ORG_BY_CODE, orgCode);
    }
    @Override
	public List<OrgEntity> queryOrgRegionList(OrgEntity param) {
		return orgDao.queryList(SQL_NAMESPACE + SQL_QUERY_FOR_LIST_REGION, param);
	}
}
